package com.example.mall.controller;


import com.alibaba.fastjson.JSON;
import com.example.mall.pojo.Attribute;
import com.example.mall.pojo.Rbody;
import com.example.mall.service.AttributeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Service
@CrossOrigin
@AllArgsConstructor
public class AttributeController {

    AttributeService attributeService;

    @DeleteMapping("categories/{id}/attributes/{attrId}")
    public Rbody deleteParams(@PathVariable("id") Integer id,@PathVariable("attrId") Integer attrId ){
        Rbody rbody = new Rbody();
            Boolean deleteAttrById = attributeService.deleteAttrById(attrId);
            if(!deleteAttrById){
                rbody.getMeta().setMsg("删除失败");
                rbody.getMeta().setStatus(400);
            }
            rbody.getMeta().setMsg("删除成功");
            rbody.getMeta().setStatus(200);
        return rbody;

    }


    @PutMapping("categories/{id}/attributes/{attrId}")
    public Rbody deleteParamsOnly(@PathVariable("id") Integer id,@PathVariable("attrId") Integer attrId,@RequestBody String body){
        Rbody rbody = new Rbody();
        System.out.println(body);
        Attribute attribute = JSON.parseObject(body, Attribute.class);

        if(attribute.getAttr_sel().equals("many")){
            Attribute attributeName = attributeService.getAttributeName(attribute.getAttr_name());
//                if(attributeName==null){
                    Boolean updateAttrById = attributeService.updateAttrById(attribute.getAttr_name(), attrId);
                    if(!updateAttrById){
                        rbody.getMeta().setMsg("更改失败");
                        rbody.getMeta().setStatus(401);
                    }
                    rbody.getMeta().setMsg("更改成功");
                    rbody.getMeta().setStatus(200);
//                }else {
//                    rbody.getMeta().setMsg("更改失败");
//                    rbody.getMeta().setStatus(400);
//                }

            }
            if(attribute.getAttr_sel().equals("only")){
                Attribute attributeNameOnly = attributeService.getAttributeNameOnly(attribute.getAttr_name());
//                if(attributeNameOnly==null){
                    Boolean updateAttrByOnly = attributeService.updateAttrByOnly(attribute.getAttr_name(),attribute.getAttr_vals(), attrId);
                    if(!updateAttrByOnly){
                        rbody.getMeta().setMsg("更改失败");
                        rbody.getMeta().setStatus(400);
                    }
                    rbody.getMeta().setMsg("更改成功");
                    rbody.getMeta().setStatus(200);
//                }else {
//                    rbody.getMeta().setMsg("更改失败");
//                    rbody.getMeta().setStatus(400);
//                }

            }




        return rbody;
    }

    @GetMapping("categories/{id}/attributes/{attrId}")
    public Rbody getParams(@PathVariable("id") Integer id,@PathVariable("attrId") Integer attrId,String attr_sel){
        Rbody rbody = new Rbody();
        if(attr_sel.equals("many")) {
            Attribute attributeByAid = attributeService.getAttributeByAid(attrId);
            if (attributeByAid == null) {
                rbody.getMeta().setMsg("获取失败");
                rbody.getMeta().setStatus(400);
            }
            rbody.setData(attributeByAid);
            rbody.getMeta().setMsg("获取成功");
            rbody.getMeta().setStatus(200);
        }else {
            Attribute attributeByAidOnly = attributeService.getAttributeByAidOnly(attrId);
            if (attributeByAidOnly == null) {
                rbody.getMeta().setMsg("获取失败");
                rbody.getMeta().setStatus(400);
            }
            rbody.setData(attributeByAidOnly);
            rbody.getMeta().setMsg("获取成功");
            rbody.getMeta().setStatus(200);
        }
        return rbody;
    }

    @PostMapping("/categories/{id}/attributes")
    public Rbody addParams(@PathVariable("id") Short id,@RequestBody String body){
        Rbody rbody = new Rbody();
        Attribute attribute = JSON.parseObject(body, Attribute.class);
        System.out.println(id);
        System.out.println("+++++++++"+body);
        Attribute attributeName = attributeService.getAttributeName(attribute.getAttr_name());
        if(attributeName==null){
            System.out.println(attribute.getAttr_name());
            System.out.println(attribute.getAttr_sel());
            Boolean addAttribute = attributeService.addAttribute(attribute.getAttr_name(), attribute.getAttr_sel(), id);
            System.out.println(addAttribute);
            if(!addAttribute){
                rbody.getMeta().setMsg("添加失败");
                rbody.getMeta().setStatus(400);
            }else{
                //        rbody.setData(attribute);
                rbody.getMeta().setMsg("添加成功");
                rbody.getMeta().setStatus(201);
            }
        }else {
            rbody.getMeta().setMsg("添加失败");
            rbody.getMeta().setStatus(400);
        }



        return rbody;

    }

    @GetMapping("categories/{id}/attributes")
    public Rbody getParamsAll(@PathVariable("id") Integer id,String sel){
        Rbody rbody = new Rbody();
        if(sel.equals("many")){
            List<Attribute> attribute = attributeService.getAttribute(id);
            System.out.println("+++++++++" + id);
            rbody.setData(attribute);
            rbody.getMeta().setStatus(200);
            rbody.getMeta().setMsg("获取成功");
        }else {
            List<Attribute> attributeByOnly = attributeService.getAttributeByOnly(id);
            rbody.setData(attributeByOnly);
            rbody.getMeta().setStatus(200);
            rbody.getMeta().setMsg("获取成功");
        }

        return  rbody;
    }


}